Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • dtable*decimal format

    Hello all,
    I am new to the group. I have a question regarding decimal formatting for dtable.
    I have two continuous variables, x and y. Is there a way to show Median (IQR) as integer for x, and 2 decimal places for y?
    Could anyone give me a code for this? Thank you very much.
    -Suteera

  • #2
    I believe the following will do it:
    Code:
    sysuse auto, clear
    
    dtable, continuous(mpg, statistics(median)) continuous(price, statistics(p50)) ///
        by(foreign) nformat(%1.0f median) nformat(%3.2f p50)
    The trick is that even though median and p50 are the same statistic, by using their different names, you are able to assign different display formats to them. Hat tip to Andrew Musau, who showed this to me.

    Comment


    • #3
      Oh it works! Thank you so much!

      Comment


      • #4
        If you find that you need to apply different numerical formats to a
        given result depending on the variable it was computed from, you can use
        command collect style cell to accomplish this. Here is a simple
        example using dtable with the auto data, where I customize the
        numeric format of the composite result (meaning all of its elements) for
        each continuous variable.
        Code:
        . sysuse auto
        (1978 automobile data)
        
        .
        . dtable mpg price head, ///
        >         define(iqi = q1 q3, trim) ///
        >         sformat("(%s)" iqi) ///
        >         define(mystats = median iqi, trim) ///
        >         continuous(, statistic(mystats))
        
        ----------------------------------------------
                                   Summary
        ----------------------------------------------
        N                                           74
        Mileage (mpg)           20.000 (18.000 25.000)
        Price          5,006.500 (4,195.000 6,342.000)
        Headroom (in.)             3.000 (2.500 3.500)
        ----------------------------------------------
        
        .
        . collect style cell var[mpg]#result[mystats], nformat(%9.0f)
        
        . collect style cell var[price]#result[mystats], nformat(%9.1f)
        
        . collect style cell var[headroom]#result[mystats], nformat(%9.2f)
        
        . collect preview
        
        -------------------------------------
                               Summary
        -------------------------------------
        N                                  74
        Mileage (mpg)              20 (18 25)
        Price          5006.5 (4195.0 6342.0)
        Headroom (in.)       3.00 (2.50 3.50)
        -------------------------------------

        Comment


        • #5
          This is exactly what I need. Thank you very much!

          Comment

          Working...
          X